/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "sopcast.h"
#include "mypopupmenu.h"
#include <sys/stat.h>
#include <sys/types.h>

//-----------------------------------------------------
void
Sopcast::loadbookmark ()
{
  ///assign initial value
  idbookmark = 2;

  QFile fp (QDir::homeDirPath () + "/.qsopcast/bookmark");
  if (fp.open (IO_ReadOnly))
    {
      QTextStream stream (&fp);
      stream.setEncoding (QTextStream::UnicodeUTF8);
      while (!stream.atEnd ())
	{
	  bookmark->insertItem (stream.readLine (), idbookmark);
	  strlst += bookmark->text (idbookmark);
	  idbookmark++;
	}
      fp.close ();
    }
}

//-----------------------------------------------------
void
Sopcast::savebookmark ()
{
  ///test change
  bool flag = FALSE;
  if (strlst.count () == bookmark->count () - 2)
    {
      QStringList::Iterator it = strlst.begin ();
      int index = 2;
      while (it != strlst.end ())
	{
	  if (bookmark->text (bookmark->idAt (index)) != *it)
	    flag = TRUE;

	  it++;
	  index++;
	}
    }
  else
    flag = TRUE;
  ///save bookmark
  if (flag == TRUE)
    {
      QFile fp (QDir::homeDirPath () + "/.qsopcast/bookmark");
      QDir dir (QDir::homeDirPath () + "/.qsopcast");
      if (!dir.exists ())
	dir.mkdir (QDir::homeDirPath () + "/.qsopcast");
      if (fp.open (IO_WriteOnly))
	{
	  QTextStream stream (&fp);
	  stream.setEncoding (QTextStream::UnicodeUTF8);

	  int index = 2;
	  while (bookmark->idAt (index) != -1)
	    {
	      stream << bookmark->text (bookmark->idAt (index)) << endl;
	      index++;
	    }
	  fp.close ();
	}
    }
}

//-----------------------------------------------------
void
Sopcast::loadopenstate ()
{
  QFile fp (QDir::homeDirPath () + "/.qsopcast/tree");
  if (fp.open (IO_ReadOnly))
    {
      ::read (fp.handle (), stateopen, sizeof (stateopen));
      fp.close ();
    }
  else
    ::memset (stateopen, 0, sizeof (stateopen));
}


//-----------------------------------------------------
void
Sopcast::saveopenstate ()
{
  updateopenstate ();

  QString data_path = QDir::homeDirPath () + "/.qsopcast/";
  QDir dir (data_path);
  if (!dir.exists ())
    dir.mkdir (data_path);
  data_path += "tree";

  QFile fp (data_path);
  if (fp.open (IO_WriteOnly))
    {
      ::write (fp.handle (), stateopen, sizeof (stateopen));
      fp.close ();
    }
  else
    perror ("path to file tree is invalid");
}

//-----------------------------------------------------
void
Sopcast::updateopenstate ()
{
  QListViewItem *myChild = listchannel->firstChild ();
  int count = 0;
  while (myChild)
    {
      if (myChild->isOpen ())
	stateopen[count] = 1;
      else
	stateopen[count] = 0;
      myChild = myChild->nextSibling ();
      count++;
    }
}
